/**
 * 
 */
package ken.app.xmlhandler;

import java.util.ArrayList;
import java.util.List;

import ken.app.bean.TreeAllBean;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * @author ken.chung
 * 
 */
public class MemberCanLikeXMLHandler extends DefaultHandler {
	
	/* 變數宣告 */
	private boolean in_item = false;
	private boolean in_tree_id = false;
	private boolean in_tree_name = false;
	private boolean in_tree_type = false;
	private boolean in_total_score = false;
	private boolean in_today_score = false;
	private boolean in_fruit_get = false;
	private boolean in_fruit_start = false;
	private boolean in_fruit_end = false;
	private boolean in_today_get = false;
	private boolean in_today_hour_min = false;
	private boolean in_today_time = false;
	private boolean in_pic1 = false;
	private boolean in_pic2 = false;
	private boolean in_lastmodify = false;
	
	private boolean in_result = false;
	
	private List<TreeAllBean> list;
	private TreeAllBean mTreeInfo;
	private String title = "";
	private StringBuffer buf = new StringBuffer();

	/* 將轉換成List<News>的XML資料回傳 */
	public List<TreeAllBean> getParsedData() {
		return list;
	}

	/* 將解析出的RSS title回傳 */
	public String getRssTitle() {
		return title;
	}

	/* XML文件開始解析時呼叫此method */
	@Override
	public void startDocument() throws SAXException {
		if (list == null) {
			list = new ArrayList<TreeAllBean>();
		}
	}

	/* XML文件結束解析時呼叫此method */
	@Override
	public void endDocument() throws SAXException {
	}

	/* 解析到Element的開頭時呼叫此method */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		
			if (localName.equals("item")) {
				this.in_item = true;
				/* 解析到item的開頭時new一個News物件 */
				mTreeInfo = new TreeAllBean();
				Log.d("ken", buf.toString());
			} else if (localName.equals("tree_id")) {
				if (this.in_item) {
					this.in_tree_id = true;
				} else {
					this.in_fruit_end = true;
				}
			} else if (localName.equals("tree_name")) {
				if (this.in_item) {
					this.in_tree_name = true;
				}
			} else if (localName.equals("tree_type")) {
				if (this.in_item) {
					this.in_tree_type = true;
				}
			} else if (localName.equals("total_score")) {
				if (this.in_item) {
					this.in_total_score = true;
				}
			} else if (localName.equals("today_score")) {
				if (this.in_item) {
					this.in_today_score = true;
				}
			} else if (localName.equals("today_get")) {
				if (this.in_item) {
					this.in_today_get = true;
				}
			}  else if (localName.equals("fruit_get")) {
				if (this.in_item) {
					this.in_fruit_get = true;
				}
			}  else if (localName.equals("today_time")) {
				if (this.in_item) {
					this.in_today_time = true;
				}
			}  else if (localName.equals("today_hour_min")) {
				if (this.in_item) {
					this.in_today_hour_min = true;
				}
			}  else if (localName.equals("fruit_start")) {
				if (this.in_item) {
					this.in_fruit_start = true;
				}
			}  else if (localName.equals("fruit_end")) {
				if (this.in_item) {
					this.in_fruit_end = true;
				}
			}  else if (localName.equals("pic1")) {
				if (this.in_item) {
					this.in_pic1 = true;
				}
			}  else if (localName.equals("pic2")) {
				if (this.in_item) {
					this.in_pic2 = true;
				}
			}  else if (localName.equals("lastmodify")) {
				if (this.in_item) {
					this.in_lastmodify = true;
				}
			}
//		}
		
		


		buf.setLength(0);
	}

	/* 解析到Element的結尾時呼叫此method */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("item")) {
			this.in_item = false;
			/* 解析到item的結尾時將News物件寫入List中 */
			list.add(mTreeInfo);
		} else if (localName.equals("tree_id")) {
			if (this.in_item) {
				/* 設定News物件的title */
				mTreeInfo.setTreeId(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_tree_id = false;
			} 
		} else if (localName.equals("tree_name")) {
			if (this.in_item) {
				/* 設定News物件的link */
				mTreeInfo.setTreeName(buf.toString().trim());
				buf.setLength(0);
				this.in_tree_name = false;
			}
		} else if (localName.equals("tree_type")) {
			if (in_item) {
				mTreeInfo.setTreeType(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_tree_type = false;
			}
		} else if (localName.equals("total_score")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mTreeInfo.setTotalScore(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_total_score = false;
			}
		} else if (localName.equals("today_score")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mTreeInfo.setTodayScore(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_today_score = false;
			}
		} else if (localName.equals("today_get")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mTreeInfo.setTodayGet(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_today_get = false;
			}
		} else if (localName.equals("today_time")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mTreeInfo.setTodayTime(Long.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_today_time = false;
			}
		}  else if (localName.equals("today_hour_min")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mTreeInfo.setTodayhourmin(buf.toString().trim());
				buf.setLength(0);
				this.in_today_hour_min = false;
			}
		}  else if (localName.equals("fruit_get")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				if (Integer.valueOf(buf.toString().trim()) == 1) {
					mTreeInfo.setFruitGet(true);
				}else{
					mTreeInfo.setFruitGet(false);
				}
				buf.setLength(0);
				this.in_fruit_get = false;
			}
		} else if (localName.equals("fruit_start")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mTreeInfo.setFruitStart(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_fruit_start = false;
			}
		} else if (localName.equals("fruit_end")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mTreeInfo.setFruitEnd(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_fruit_end = false;
			}
		} else if (localName.equals("pic1")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mTreeInfo.setPic1(buf.toString().trim());
				buf.setLength(0);
				this.in_pic1 = false;
			}
		} else if (localName.equals("pic2")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mTreeInfo.setPic2(buf.toString().trim());
				buf.setLength(0);
				this.in_pic2 = false;
			}
		} else if (localName.equals("lastmodify")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mTreeInfo.setLastmodify(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_lastmodify = false;
			}
		}
	}

	/* 取得Element的開頭結尾中間夾的字串 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_item || this.in_lastmodify) {
			/* 將char[]加入StringBuffer */
			buf.append(ch, start, length);
		}
	}
}